//
//  Created by zlcode on 2017/4/19.
//  Copyright © 2017年 zlcode. All rights reserved.
//

#import "BrightnessVolumeView.h"
typedef NS_ENUM(NSInteger,GestureType) {
    none_type = 0,
    voice_type ,
    brightness_type ,
    schedule_type
};
@interface BrightnessVolumeView()

@property (nonatomic, assign)GestureType    gestureType;

@end
@implementation BrightnessVolumeView
{
    CGFloat _lastPanX;
    CGFloat _lastPanY;
    /** 手势只做一次判断 */
    CGFloat _panStarte;
}
#pragma mark - xib初始化入口
- (id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self addPanGesture];
        [BrightnessView sharedBrightnessView];
    }
    return self;
}

#pragma mark - 代码初始化入口
- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addPanGesture];
        _panStarte = 0;
        _gestureType = none_type;
        [BrightnessView sharedBrightnessView];
    }
    return self;
}

#pragma mark - 添加拖动手势
- (void)addPanGesture{
    // 拖动手势
    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [self addGestureRecognizer:panRecognizer];
}

#pragma mark -  处理音量和亮度
- (void)handlePan:(UIPanGestureRecognizer*)recognizer
{
    CGPoint location = [recognizer locationInView:self];
    CGPoint point = [recognizer translationInView:recognizer.view];
    
    switch (recognizer.state) {
        case UIGestureRecognizerStateBegan:
        {
            _lastPanX = point.x;
            _lastPanY = point.y;
        }
            break;
        case UIGestureRecognizerStateChanged:
        {
            if ((point.y - _lastPanY > point.x - _lastPanX && (point.y - _lastPanY) >= 0 && point.x - _lastPanX >= 0)  || (point.y - _lastPanY < point.x - _lastPanX && (point.y - _lastPanY) <= 0 && point.x - _lastPanX <= 0) ) {
                if (_panStarte == 0) {
                    _panStarte = 1;
                    if (location.x > ZLScreenWidth * 0.5) {
                        _gestureType = voice_type;
                        self.lastVolume = [self bfGetCurrentVolume];
                        NSLog(@"_gestureType   voice_type");
                        
                    }else{
                        _gestureType = brightness_type;
                        self.lastBrightness = [UIScreen mainScreen].brightness;
                        NSLog(@"_gestureType   brightness_type");
                    }
                }
                
                
                
            }else{
                if (_panStarte == 0 && (_lastPanX - point.x > 30 || _lastPanX - point.x < -30) && ((_lastPanY - point.y < 10 && _lastPanY - point.y > 0) || (_lastPanY - point.y > - 10 && _lastPanY - point.y < 0))) {
                    NSLog(@"_gestureType   schedule_type");
                    _panStarte = 1;
                    _gestureType = schedule_type;
                }
                if (_panStarte == schedule_type) {
//                    NSInteger value = (_lastPanX - point.x )/5;
//                    if (self.delegate && [self.delegate respondsToSelector:@selector(ChangeValue:WithType:)]) {
//                        [self.delegate ChangeValue:value WithType:brightness_type];
//                    }
                }
                
            }
            switch (_gestureType) {
                case voice_type:
                {
                    
                    
                    float volumeDelta = point.y / (recognizer.view.bounds.size.height) *2;
                    float newVolume = self.lastVolume - volumeDelta;
                    
                    [self bfSetVolume:newVolume];
                }
                    break;
                case brightness_type:
                {
                    
                    float volumeDelta = point.y / (recognizer.view.bounds.size.height) *2;
                    float newVolume = self.lastBrightness - volumeDelta;
                    
                    [[UIScreen mainScreen] setBrightness:newVolume];
                }
                    break;
                case schedule_type:
                {
                    float volumeDelta = (point.x - _lastPanX) / (recognizer.view.bounds.size.width) *50;
                    if (self.brightnessDelegate && [self.brightnessDelegate respondsToSelector:@selector(BrightnessViewsSheduleForVideo:)]) {
                        [self.brightnessDelegate BrightnessViewsSheduleForVideo:volumeDelta];
                    }
                    
                   
                }
                    break;
                    
                default:
                    break;
            }
        }
            break;
        case UIGestureRecognizerStateEnded: {
            if (_gestureType == schedule_type) {
                float volumeDelta = point.x / (recognizer.view.bounds.size.width) *50;
                if (self.brightnessDelegate && [self.brightnessDelegate respondsToSelector:@selector(BrightnessViewsSheduleEndForVideo:)]) {
                    [self.brightnessDelegate BrightnessViewsSheduleEndForVideo:volumeDelta];
                }
            }
            _panStarte = 0;
            _gestureType = none_type;
            
        }
            break;
        case UIGestureRecognizerStateCancelled: {
            _panStarte = 0;
            _gestureType = none_type;
        }
            break;
        default:
            break;
    }
    
    
    
    
    
    
    
    
    
    // 音量
//    if (location.x > ZLScreenWidth * 0.5) {
//
//        if (recognizer.state == UIGestureRecognizerStateBegan) {
//            self.lastVolume = [self bfGetCurrentVolume];
//        }
//
//        float volumeDelta = point.y / (recognizer.view.bounds.size.height) * 0.5;
//        float newVolume = self.lastVolume - volumeDelta;
//
//        [self bfSetVolume:newVolume];
//
//    } else {// 亮度
//
//        if (recognizer.state == UIGestureRecognizerStateBegan) {
//            self.lastBrightness = [UIScreen mainScreen].brightness;
//        }
//
//        float volumeDelta = point.y / (recognizer.view.bounds.size.height) * 0.5;
//        float newVolume = self.lastBrightness - volumeDelta;
//
//        [[UIScreen mainScreen] setBrightness:newVolume];
//    }
}

-(void)panGesture:(UIPanGestureRecognizer *)pan {
    
    
   
    
}
#pragma mark -  兼容iOS 7.0前后的音量控制
- (float)bfGetCurrentVolume {
    // 通过控制系统声音 控制音量
    if (ZLSystemVersion >= 7) {
        if (_volumeViewSlider) {
            return _volumeViewSlider.value;
        }
        MPVolumeView *volumeView = [[MPVolumeView alloc] init];
        for (UIView *view in [volumeView subviews]){
            if ([view.class.description isEqualToString:@"MPVolumeSlider"]){
                _volumeViewSlider = (UISlider*)view;
                break;
            }
        }
        return _volumeViewSlider.value;
    } else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        // 过期api写在这里不会有警告
        return [[MPMusicPlayerController applicationMusicPlayer] volume];
#pragma clang diagnostic pop
    }
}

#pragma mark - 控制音量
- (void)bfSetVolume:(float)newVolume {
    // 通过控制系统声音 控制音量
    newVolume = newVolume > 1 ? 1 : newVolume;
    newVolume = newVolume < 0 ? 0 : newVolume;
    
    if (ZLSystemVersion >= 7) {
        [self.volumeViewSlider setValue:newVolume animated:NO];
    } else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        // 过期api写在这里不会有警告
        [[MPMusicPlayerController applicationMusicPlayer] setVolume:newVolume];
#pragma clang diagnostic pop
    }
}

@end
